import axios from "axios";
import {
    change_page_total_count,
    save_author_article_list,
    save_pub_account_author,
    update_choice_author_index
} from "./contants";

export const requestPubAuthors =()=>{

    return (dispatch) =>{
       axios.get("/wxarticle/chapters/json")
           .then((res) =>{
               const data = res.data.data;
               console.log(data);
               const action = saveAuthorsList(data);
               dispatch(action);
               const id = data[0].id;
               const artAction = requestAuthorArticleList(id,0);
               dispatch(artAction);
           })
    }
};

const saveAuthorsList =(data) =>({
    type:save_pub_account_author,
    data:data,
});


export const updateChoiceAuthorIndex =(index)=>({
    type: update_choice_author_index,
    index:index
});


export const requestAuthorArticleList =(id,pageIndex)=>{

    return (dispatch)=>{
        axios.get(`/wxarticle/list/${id}/${pageIndex}/json`)
            .then((res) =>{
                console.log(`获取公众号 文章列表`);
                console.log(res.data);

                const datas = res.data.data.datas;

                const pageCount = res.data.data.pageCount;
                console.log(pageCount);
                const pageCountAction = changePageCount(pageCount);
                dispatch(pageCountAction);

                const  action = saveAuthorArticleList(id,pageIndex,datas);
                dispatch(action);
            })

    }
};
// 一个页码 对应一段 列表   //后续要做成 保存所有点击的数据然后保存起来
export const  saveAuthorArticleList = (id,pageIndex,datas)=>({
    type:save_author_article_list,
    id:id,
    pageIndex:pageIndex,
    data:datas,
});

const  changePageCount = (pageCount) =>({
    type:change_page_total_count,
    pageCount:pageCount
});